<template>
  <div>
    <el-dialog
      title="班级列表"
      :visible.sync="dialogClassVisible"
      width="30%"
    >
      <el-radio-group v-model="checkedList" @input="changeCheck">
        <el-radio
          v-for="item in pushClassList"
          :key="item.id"
          style="display:block;"
          :label="item.id"
        >{{ item.class_name }}</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClassVisible = false">取 消</el-button>
        <el-button type="primary" @click="pushClass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getClassList, bindClass } from '@/api/exam/exam.js'
export default {
  name: 'ExamPush',
  props: {
    classList: {
      type: Array,
      default: () => []// 单选框渲染组
    }
  },
  data() {
    return {
      checkedList: [], // 回显默认选中数组
      dialogClassVisible: false, // 控制弹框显示隐藏
      pushClassList: [], // 可以推送的班级列表数据
      checkId: 0, // 班级id
      main_paper_id: 0, // 试卷id
      subject_id: 0, // 科目id
      exam_id: 0// 考试id
    }
  },
  // created() {
  //   this.getList()
  // },
  methods: {
    async getList(row) {
      // console.log(this.classList, '班级数据')
      this.subject_id = row.subject_id
      this.main_paper_id = row.main_paper_id
      this.exam_id = row.id
      // 调接口拿到所有可以推送的班级
      const res = await getClassList({
        state: 1,
        subject_id: this.subject_id
      })
      console.log(row, '当前操作')
      this.pushClassList = res.list
    },
    // 当选项变化的时候拿到选中的id
    changeCheck(id) {
      this.checkId = id
      console.log(this.checkId)
    },
    // class_id,exam_id,main_paper_id===绑定
    // 推送班级考试
    async pushClass() {
      const res = await bindClass({
        class_id: this.checkId,
        exam_id: this.exam_id,
        main_paper_id: this.main_paper_id
      })
      console.log(res, '推送结果')
      // 提示成功
      this.$message.success('推送成功')
      // 关闭弹框
      this.dialogClassVisible = false
      // console.log(this.checkedList, '=====')
      this.checkedList = []
      // 刷新列表
      this.$parent.getExamList()
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-radio{
  padding: 20px 0 0 30px;
}
</style>
